package popUtil;import java.util.List;

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.administrator.sunstart_library.R;

import SunStarUtils.UtilBox;


/** 
 * @author  作者: 孙浩
 * @date 创建时间：2016年5月24日 下午7:39:50 
 * 弹出窗工具类  
 */
public class ShowPop {
	private Context context;
	private List<PopList> text;
	private PopupWindow window;
	private View upView;	
	private ListView popListView;
	private String colorString;
	private PopAdapter adapter;
	private String status;
	private TextView setTextTV;
	private IPopCallBack call;
	private boolean onceClick = true;
	private int match ;

	/**
	 * 
	 * @param context 资源文件
	 * @param text	状态集合
	 * @param upView 顶部View
	 * @param colorString 弹出窗颜色
	 */
	public ShowPop(Context context, List<PopList> text, View upView,
			String colorString,IPopCallBack iPopCallBack) {
		super();
		this.context = context;
		this.text = text;
		this.upView = upView;
		this.colorString = colorString;		
		this.call = iPopCallBack;
	}


	public void setOnceClick(boolean onceClick) {
		this.onceClick = onceClick;
	}

	public PopupWindow getWindow() {
		return window;
	}

	public void setMatch(int match) {
		this.match = match;
	}

	/**
	 * 设置pop显示
	 */
	public void setpop() {
		// TODO Auto-generated method stub
		View popView = View.inflate(context,
				R.layout.poplistview, null);
		if(match == 0)
		window = new PopupWindow(popView, upView.getWidth(),
				LayoutParams.WRAP_CONTENT, true);
		else if(match == 1)
			window = new PopupWindow(popView, LayoutParams.MATCH_PARENT,
					LayoutParams.WRAP_CONTENT, true);
		else if(match == 2){
			window = new PopupWindow(popView, LayoutParams.MATCH_PARENT,
					LayoutParams.MATCH_PARENT, true);
		}
		else
			window = new PopupWindow(popView, LayoutParams.MATCH_PARENT,
					UtilBox.getWindowHeight((Activity) context)/3, true);
		popListView = (ListView) popView.findViewById(R.id.poplistview);		
		LinearLayout popLayout = (LinearLayout) popView.findViewById(R.id.poplistview_layout);
		ColorDrawable color = new ColorDrawable(Color.parseColor(colorString));
		adapter = new PopAdapter(text, context);
		popListView.setAdapter(adapter);
		window.setBackgroundDrawable(color);
		window.setContentView(popView);
		window.showAsDropDown(upView);
		popLayout.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View view, MotionEvent motionEvent) {
				window.dismiss();
				return true;
			}
		});
		setListener();
	}

	/**
	 * 设置listview点击事件
	 */
	public void setListener() {
		// TODO Auto-generated method stub
		popListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				status = text.get(position).getStatus();
				if(onceClick == true){
					for (int i = 0 ; i < text.size() ; i ++){
						text.get(i).setCheck(false);
					}
					text.get(position).setCheck(true);
					window.dismiss();
					if(call!=null){
						call.callBack(status, text.get(position).getText());
					}
				}else{

				}
			}
		});
	}
	
	public String getStatus() {
		return status;
	}
	
	
	
}



class PopAdapter extends BaseAdapter {
	private List<PopList> data;
	private Context context;
	
	public PopAdapter(List<PopList> data, Context context) {
		super();
		this.data = data;
		this.context = context;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		if(convertView == null){
			holder = new ViewHolder();
			convertView = View.inflate(context, R.layout.adapter_poplistview, null);
			holder.poptext = (TextView) convertView.findViewById(R.id.popadapter_tv);
			convertView.setTag(holder);
		}else {
			holder = (ViewHolder) convertView.getTag();
		}
			holder.poptext.setText(data.get(position).getText());
		if(data.get(position).isCheck() == true){
			holder.poptext.setBackgroundColor(context.getResources().getColor(R.color.white));
			holder.poptext.setTextColor(context.getResources().getColor(R.color.textorg));
		}else{
			holder.poptext.setBackgroundColor(context.getResources().getColor(R.color.white));
			holder.poptext.setTextColor(context.getResources().getColor(R.color.textBlcak));
		}
		return convertView;
	}
	class ViewHolder{
		TextView poptext;
	}
	
	
}